package com.parking.order.controller;


import cn.hutool.core.bean.BeanUtil;
import com.parking.common.core.model.PageInfo;
import com.parking.common.core.model.Result;
import com.parking.common.core.util.JwtUtil;
import com.parking.order.model.dto.*;
import com.parking.order.model.param.AddCommentParam;
import com.parking.order.model.param.FindCommentByOrderNumParam;
import com.parking.order.model.param.OrderCommentParam;
import com.parking.order.model.param.ReplyCommentParam;
import com.parking.order.model.form.*;
import com.parking.order.service.IOrderCommentService;
import com.parking.order.util.BasicInfoUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 *  订单评论控制层
 * </p>
 *
 * @author 吴彦祖
 * @since 2023年11月28日
 */
@RestController
@Api(tags = "订单评论")
@RequestMapping("/orderComment")
public class OrderCommentController {
    /**
     * 订单评论业务层
     * */
    @Autowired
    private IOrderCommentService orderCommentService;
    /**
     * 其他模块服务调用
     * */
    @Autowired
    private BasicInfoUtil basicInfoUtil;
    /**
     * 用户添加订单评论
     * 参数 authorization 用户token
     *      orderInfoNum 订单编号
     *      commentInfo 评论内容
     *      commentScore 评论分数
     * 返回 commentId 评论Id
     *     orderInfoNum 订单编号
     *     commentInfo 评论内容
     *     commentScore 评论分数
     *     commentTime 评论时间
     *     certifiedInfoId 物业信息Id
     *     certifiedInfoName 物业名称
     *     certifiedInfoTel  物业电话
     *     certifiedInfoAddress 物业地址
     * */
    @ApiOperation("用户添加订单评论")
    @PutMapping("/addComment.user")
    public Result<AddCommentDto> addComment(@RequestBody @Valid AddCommentForm form, @RequestHeader String authorization) throws Exception {
        AddCommentParam param = BeanUtil.toBean(form, AddCommentParam.class);
        param.setUserId(JwtUtil.getUserId(authorization));
        AddCommentDto dto = orderCommentService.addComment(param);
        return Result.getResult(AddCommentDto.class).setData(dto).setMessage("用户添加评论成功");
    }
    /**
     * 物业回复订单评论
     * 参数 authorization 物业token
     *     commentId     订单评论Id
     *     replyInfo     回复内容
     * 返回 commentId 评论Id
     *     orderInfoNum 订单编号
     *     commentInfo 评论内容
     *     commentScore 评论分数
     *     commentTime 评论时间
     *     replyInfo   回复内容
     *     replyTime   回复时间
     *     userInfoId  用户信息Id
     *     userInfoName 用户姓名
     * */
    @ApiOperation("物业回复订单评论")
    @PutMapping("/replyComment.certified")
    public Result<ReplyCommentDto> replyComment(@RequestBody @Valid ReplyCommentForm form, @RequestHeader String authorization) throws Exception {
        ReplyCommentParam param = BeanUtil.toBean(form, ReplyCommentParam.class);
        param.setCertifiedId(JwtUtil.getCertifiedAccountId(authorization));
        ReplyCommentDto dto = orderCommentService.replyComment(param);
        return Result.getResult(ReplyCommentDto.class).setData(dto).setMessage("物业回复评论成功");
    }

    /**
     * 平台查询有回复评论
     * 参数 是否回复 isReply
     *     订单编号 orderInfoNum
     *     用户信息Id userInfoId
     *     开始时间 startTime
     *     结束时间 endTime
     *     订单评分 commentScore
     *     物业信息Id certifiedInfoId
     * 返回 评论Id commentId
     *     订单编号 orderInfoNum
     *     评论内容 commentInfo
     *     评论时间 commentTime
     *     评论分数 commentScore
     *     回复内容 replyInfo
     *     回复时间 replyTime
     *     物业信息Id certifiedInfoId
     *     物业名称 certifiedInfoName
     *     物业电话 certifiedInfoTel
     *     物业地址 certifiedInfoAddress
     *     用户信息Id userInfoId
     *     用户姓名   userInfoName
     *     用户电话   userInfoTel
     * */
    @ApiOperation("平台查询评论")
    @GetMapping("/findComment.platform")
    public Result<PageInfo> findOrderCommentPlatform(@Valid OrderCommentPlatformForm form, @RequestHeader String authorization) throws Exception {
        OrderCommentParam param = BeanUtil.toBean(form, OrderCommentParam.class);
        JwtUtil.parseAdminToken(authorization);
        PageInfo page =  orderCommentService.findOrderCommentPlatform(param);
        return Result.getResult(PageInfo.class).setData(page).setMessage("平台查询评论成功");
    }

    /**
     * 物业查询有回复评论
     * 参数 是否回复 isReply
     *     订单编号 orderInfoNum
     *     用户信息Id userInfoId
     *     开始时间 startTime
     *     结束时间 endTime
     *     订单评分 commentScore
     * 返回 评论Id commentId
     *     订单编号 orderInfoNum
     *     评论内容 commentInfo
     *     评论时间 commentTime
     *     评论分数 commentScore
     *     回复内容 replyInfo
     *     回复时间 replyTime
     *     用户信息Id userInfoId
     *     用户姓名   userInfoName
     *     用户电话   userInfoTel
     * */
    @ApiOperation("物业查询评论")
    @GetMapping("/findComment.certified")
    public Result<PageInfo> findOrderCommentCertified(@Valid OrderCommentCertifiedForm form, @RequestHeader String authorization) throws Exception {
        OrderCommentParam param = BeanUtil.toBean(form, OrderCommentParam.class);
        Integer certifiedId = JwtUtil.getCertifiedAccountId(authorization);
        param.setCertifiedInfoId(basicInfoUtil.getCertifiedAccount(certifiedId).getCertifiedInfoId());
        PageInfo page =  orderCommentService.findOrderCommentCertified(param);
        return Result.getResult(PageInfo.class).setData(page).setMessage("物业查询评论成功");
    }

    /**
     * 用户根据订单编号查询订单评论
     * 参数 用户token authorization
     *      订单编号 orderInfoNum
     * 返回  评论内容 commentInfo
     *      评论时间 commentTime
     *      回复内容 replyInfo
     *      回复时间 replyTime
     *      订单评分 commentScore Good Bad OK
     * */
    @ApiOperation("用户根据订单编号查询订单评论")
    @GetMapping("/findCommentByOrderNum.user")
    public Result<CommentDto> findCommentByOrderNumUser(@Valid FindCommentByOrderNumUserForm form, @RequestHeader String authorization) throws Exception {
        FindCommentByOrderNumParam param = BeanUtil.toBean(form, FindCommentByOrderNumParam.class);
        param.setUserId(JwtUtil.getUserId(authorization));
        CommentDto dto = orderCommentService.findCommentByOrderNum(param);
        return Result.getResult(CommentDto.class).setMessage("用户根据订单编号查询订单评论").setData(dto);
    }

    /**
     * 出租客根据订单编号查询订单评论
     * 参数 出租客token authorization
     *      订单编号 orderInfoNum
     * 返回  评论内容 commentInfo
     *      评论时间 commentTime
     *      回复内容 replyInfo
     *      回复时间 replyTime
     *      订单评分 commentScore Good Bad OK
     * */
    @ApiOperation("出租客根据订单编号查询订单评论")
    @GetMapping("/findCommentByOrderNum.renters")
    public Result<CommentDto> findCommentByOrderNumRenters(@Valid FindCommentByOrderNumRentersForm form, @RequestHeader String authorization) throws Exception {
        FindCommentByOrderNumParam param = BeanUtil.toBean(form, FindCommentByOrderNumParam.class);
        param.setRentersId(JwtUtil.getRentersId(authorization));
        CommentDto dto = orderCommentService.findCommentByOrderNum(param);
        return Result.getResult(CommentDto.class).setMessage("出租客根据订单编号查询订单评论").setData(dto);
    }
}

